package sng.net.http
{
	import flash.utils.ByteArray;
	
	import sng.net.http.cspkg.CSPKGMSG;
	import sng.net.http.cspkg.ErrorMap;
	import sng.net.http.cspkg.Macros;
	import sng.net.http.cspkg.QQUserSimple;
	import sng.net.http.cspkg.TCommonExtU;
	import sng.net.http.cspkg.TStopExt;
	import sng.net.http.cspkg.TWebExt;
	import sng.net.http.cspkg.TWebPkg;

	public class TDRUtil
	{
		
		/**
		 * 
		 */		
		static private var _instance:TDRUtil = null;
		
		/**
		 * 
		 */		
		private var _webPkg:TWebPkg;
		
		/**
		 * 
		 */		
		private var _buf:ByteArray;
		
		
		public function TDRUtil()
		{
			_webPkg = new TWebPkg();
			
			_buf = new ByteArray();
			
			initMsgPkg();
			
		}
		
		static public function get instance():TDRUtil
		{
			if(null == _instance)
			{
				_instance = new TDRUtil();
			}
			
			return _instance;
		}
		
		
		/**
		 * TDR写入buf 
		 * @param msg
		 * @param buf
		 * 
		 */		
		public function encode(msg:CSPKGMSG):ByteArray
		{
			if(!msg)
			{
				trace("TDR encode msg == null");
				return null;
			}
			
			// 这里decode会修改
			setMsgPkg();
			
			// 清空buf
			clearBuf();

//			var csVec:Vector.<CSPKGMSG> = new  Vector.<CSPKGMSG>(1, true);
//			csVec[0]=msg;
			_webPkg.Body =msg;// _webPkg.decode(msg.encode(;

			var ret:int = _webPkg.encode(_buf);
			
			if(ErrorMap.NO_ERROR != ret)
			{
				trace("TDR encode error == " + ErrorMap.getErrorString(ret));
				return null;
			}
		
			trace(_buf.length.toString())
			return _buf;
			
		}
		
		/**
		 * TDR读取buf 
		 * @param buf
		 * @return 
		 * 
		 */		
		public function decode(buf:ByteArray):CSPKGMSG
		{
			if(!buf)
			{
				trace("TDR decode buf == null");
				return null;
			}
			
			var ret:int = _webPkg.decode(buf);
			
			if(ErrorMap.NO_ERROR != ret)
			{
				trace("TDR decode error == " + ErrorMap.getErrorString(ret));
				return null;
			}
			
			return _webPkg.Body;
			
		}
		
		
		private function initMsgPkg():void
		{
			_webPkg.Head.Ext.StopExt = new TStopExt();	//新修改的
		var qquserSimple:QQUserSimple=new QQUserSimple();
		qquserSimple.Uin=1230078;
		qquserSimple.SKey="1423120000";
			_webPkg.Head.Base.User.qqUserSimple=qquserSimple;
		}
		
		private function setMsgPkg():void
		{
			_webPkg.Head.Base.Magic = Macros.TWEB_MAGIC;
			_webPkg.Head.Base.Version = Macros.TWEB_VERSION;
			_webPkg.Head.Base.Cmd = Macros.TWEB_CMD_UPLOAD;
			//_webPkg.Head.Base.BodyLen = 1;
			_webPkg.Head.Base.UserType = Macros.TWEB_USER_QQSIMPLE;
//			_webPkg.Head.Base.Seq = 0;
			
			// 这里通过JS获取cookie
//			_webPkg.Head.Ext.CommonExtU.Uin = 10000000;
//			var intVec:Vector.<int> = new  Vector.<int>(11, true);
//			
//			
//			for (var Passwd_i : uint = 0; Passwd_i < 11; Passwd_i++) {
//				intVec[Passwd_i] == 1;
//			}
//			_webPkg.Head.Ext.CommonExtU.SKey = intVec;
		}
		
		private function clearBuf():void
		{
			_buf.position = 0;
			_buf.length = 0;
		}
		
	}
}